<template>
    <div class="context">

        <div class="select">
            <el-select v-model="value" placeholder="请选择">
                <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value">
                </el-option>
            </el-select>

            <el-input style="width:150px; margin: 0px 0px 0px 20px;" v-model="inputvalue" placeholder="请输入内容">
            </el-input>
            <el-button type="primary" @click="toselect" style="height:40px; margin: 0px 0px 0px 20px;">
                立即查询</el-button>
            <el-button @click="reset" type="success" style="height:40px; margin: 0px 0px 0px 20px;">
                重置 </el-button>
            <el-button type="primary" @click="dialogFormVisible = true" style="height:40px; margin: 0px 0px 0px 20px;">
                新增班级</el-button>
        </div>
        <div>

            <el-dialog title="修改信息" :visible.sync="dialogresetVisible">
                <el-form :model="newtable">
                    <el-form-item label="班级名称:" :label-width="formLabelWidth">
                        <el-input v-model="newtable.classname" autocomplete="off"></el-input>
                    </el-form-item>

                    <el-form-item label="班级地址:" :label-width="formLabelWidth">
                        <el-input v-model="newtable.classplace" autocomplete="off"></el-input>
                    </el-form-item>

                    <el-form-item label="班级教师:" :label-width="formLabelWidth">
                        <el-input v-model="newtable.classteachername" autocomplete="off"></el-input>
                    </el-form-item>

                    <el-form-item label="班级状态:" :label-width="formLabelWidth">
                        <el-select v-model="statevalue" placeholder="请选择">
                            <el-option v-for="item in stateoptions" :key="item.value" :label="item.label"
                                :value="item.value">
                            </el-option>
                        </el-select>
                    </el-form-item>



                </el-form>
                <div slot="footer" class="dialog-footer">
                    <el-button @click="dialogresetVisible = false">取 消</el-button>
                    <el-button type="primary" @click="update">确 定</el-button>
                </div>
            </el-dialog>

            <el-dialog title="新增班级" :visible.sync="dialogFormVisible">
                <el-form :model="newclass">
                    <el-form-item label="班级名称" :label-width="formLabelWidth">
                        <el-input v-model="newclass.classname" autocomplete="off"></el-input>
                    </el-form-item>

                    <el-form-item label="班级地址" :label-width="formLabelWidth">
                        <el-input v-model="newclass.classplace" autocomplete="off"></el-input>
                    </el-form-item>

                    <el-form-item label="班级教师" :label-width="formLabelWidth">
                        <el-input v-model="newclass.classteachername" autocomplete="off"></el-input>
                    </el-form-item>

                </el-form>
                <div slot="footer" class="dialog-footer">
                    <el-button @click="dialogFormVisible = false">取 消</el-button>
                    <el-button type="primary" @click="add">确 定</el-button>
                </div>
            </el-dialog>


        


            <template>
                <el-table :data="tableData" style="width: 100%" height="450">
                    v-loading="loading"
                    border>

                    <el-table-column label="班级ID" width="180">
                        <template slot-scope="scope">

                            <span style="margin-left: 10px">{{ scope.row.classid }}</span>
                        </template>
                    </el-table-column>

                    <el-table-column label="班级名称" width="180">
                        <template slot-scope="scope">

                            <span style="margin-left: 10px">{{ scope.row.classname }}</span>
                        </template>
                    </el-table-column>



                    <el-table-column label="班级当前人数" width="180">
                        <template slot-scope="scope">

                            <span style="margin-left: 10px">{{ scope.row.classpeople }}</span>
                        </template>
                    </el-table-column>

                    <el-table-column label="班级地点" width="180">
                        <template slot-scope="scope">

                            <span style="margin-left: 10px">{{ scope.row.classplace }}</span>
                        </template>
                    </el-table-column>

                    <el-table-column label="班级状态" width="180">
                        <template slot-scope="scope">

                            <el-tag v-show="scope.row.classstate === 1 ? true : false"
                                :type="scope.row.classstate === -1 ? 'primary' : 'success'" disable-transitions>
                                开课中</el-tag>


                            <el-tag v-show="scope.row.classstate === -1 ? true : false"
                                :type="scope.row.classstate === -1 ? 'primary' : 'success'" disable-transitions>
                                已结课</el-tag>

                            <!-- <span style="margin-left: 10px" v-show="scope.row.classstate===-1?true:false">已结课</span>
                            <span style="margin-left: 10px"  v-show="scope.row.classstate===1?true:false">开课中</span> -->
                        </template>
                    </el-table-column>

                    <el-table-column label="任课老师" width="180">
                        <template slot-scope="scope">

                            <span style="margin-left: 10px">{{ scope.row.classteachername }}</span>
                        </template>
                    </el-table-column>

                    <el-table-column label="操作" width="250">

                        <!-- <el-button @click="dialogFormVisible = true">新增班级</el-button>
                        <el-button @click="handleClick(scope.row)" type="text" size="small">查看</el-button>
                        <el-button type="text" size="small">编辑</el-button> -->


                        <template slot-scope="scope">
                            <!-- <el-button type="text" size="small" @click="showstudent(scope.$index, scope.row)">查看学生
                            </el-button> -->
                            <el-button type="text" size="small" @click="intoclass(scope.$index, scope.row)">进入班级
                            </el-button>
                            <el-button type="text" size="small" @click="handleEdit(scope.$index, scope.row)">编辑班级
                            </el-button>
                            <el-button type="text" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
                        </template>



                    </el-table-column>

                </el-table>
                <div class="block">

                    <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
                        :current-page="currentPage4" :page-sizes="[10, 20, 30, 40]" :page-size="100"
                        layout="total, sizes, prev, pager, next, jumper" :total=999>
                    </el-pagination>
                </div>
            </template>
        </div>
    </div>
</template>

<script>

export default {
    data() {
        return {
            inputvalue: null,
            options: [{
                value: 'state',
                label: '是否结课 1：开课中 -1:已结课'
            }, {
                value: 'teachername',
                label: '教师名称查询'
            }, {
                value: 'place',
                label: '班级地点查询'
            }, {
                value: 'classname',
                label: '班级名称查询'
            }],
            value: '',
            req: {
                "classname": null,
                "classpeople": null,
                "classplace": null,
                "classstate": null,
                "classteachername": null,
                "startIndex": 0,
                "pageSize": 10
            },
            pagecount: 99,//查询的总数
 
            //班级状态选择
            stateoptions: [{
                value: 1,
                label: '开课中'
            }, {
                value: -1,
                label: '结课'
            }],

            statevalue: '',
            //新增的班级数据
            newclass: {
                classname: 'newclass',
                classplace: 'newclass',
                classstate: 1,
                classteachername: "wzh",
                classpeople: 1,
            },

            formLabelWidth: '120px',
            dialogFormVisible: false,
            dialogFormVisiblestudent: false,//查看学生面板
            dialogresetVisible: false,
            dialogresetVisibleaddstudent: false,//增加学生面板
            //修改提交的
            newtable: {
                classid: null,
                classname: null,
                classplace: null,
                classstate: null,
                classteachername: null,
                classpeople: null,
            },
            //表单数据容器
            tableData: [{
                classid: 1,
                classname: '默认数据',
                classplace: '默认数据',
                classstate: 9999,
                classteachername: "wzh",
                classpeople: 9999,
            }],
            currentPage1: 5,
            currentPage2: 5,
            currentPage3: 5,
            currentPage4: 4
        }
    },

    created() {
        var that = this
        console.log('Vue 初始化完成 created')
        this.pushdata()
    },
    methods: {

        //条件查询
        toselect() {
            var that = this
            this.req = {
                "classname": null,
                "classpeople": null,
                "classplace": null,
                "classstate": null,
                "classteachername": null,
                "startIndex": 0,
                "pageSize": 10
            }
            if (this.value == 'state') {
                that.req.classstate = that.inputvalue
            }
            if (this.value == 'teachername') {
                that.req.classteachername = that.inputvalue
            }
            if (this.value == 'place') {
                that.req.classplace = that.inputvalue
            }
            if (this.value == 'classname') {
                that.req.classname = that.inputvalue
            }

            this.pushdata()
        },
        reset() {
            this.value = ""
            this.inputvalue = ""
            this.req = {
                "classname": null,
                "classpeople": null,
                "classplace": null,
                "classstate": null,
                "classteachername": null,
                "startIndex": 0,
                "pageSize": 10
            }

        },
        handleSizeChange(val) {
            console.log(`每页 ${val} 条`);
            this.reset()
            this.req.pageSize = val
            this.pushdata()
        },
        handleCurrentChange(val) {
            console.log(`当前页: ${val}`);

            if (val != 1) {
                var statcount = (val - 1) * this.req.pageSize
                this.req.startIndex = statcount
                console.log("当前开始index--" + statcount);
            } else {
                this.req.startIndex = 0
                console.log("当前开始index--" + statcount);
            }

            console.log(this.req);
            this.pushdata()
        },
        //进入班级
        intoclass(index, row) {
            this.$router.push({name:"classdetail", query:{
                classid:row.classid,
                classstate:row.classstate,
                classpeople:row.classpeople,
                classteachername:row.classteachername,
                classname:row.classname
            }})
        },
        //编辑班级信息
        handleEdit(index, row) {
            var that = this  
            that.statevalue = row.classstate
            that.newtable = {
                classid: row.classid,
                classname: row.classname,
                classplace: row.classplace,
                classstate: row.classstate,
                classteachername: row.classteachername,
                classpeople: row.classpeople,
            }
            
            this.dialogresetVisible = true
        },
        //删除班级
        handleDelete(index, row) {
            var that = this
            console.log(index, row);
            this.$axios.post('/class/deleteclass', {
                classid: row.classid
            }).then(function (response) {
                console.log(response);
                that.$message({
                    message: '删除成功!',
                    type: 'success'
                });
                that.pushdata()
            })
                .catch(function (error) {
                    console.log(error);
                    that.$message({
                        message: '添加失败'
                    });
                });
        },
        open1() {
            this.$message({
                message: '添加成功!',
                type: 'success'
            });
        },
        open2() {
            this.$message({
                message: '添加失败'
            });
        },
        //刷新表格数据
        pushdata() {
            var that = this
            this.$axios.post('/class/classlist', that.req)
                .then(function (response) {
                    console.log(response.data);
                    that.tableData = response.data.data
                    that.pagecount = response.data.pageCount
                })
                .catch(function (error) {
                    console.log(error);
                    this.$message({
                        message: '查询失败'
                    });
                });

        },
        //修改班级信息
        update() {
            var that = this
            that.newtable.classstate=that.statevalue
            console.log(that.newtable);
            that.$axios.post('/class/updateclass', that.newtable).then(function (response) {
                console.log(response);
                that.dialogresetVisible = false
                that.pushdata()
                that.$message({
                    message: '修改成功!',
                    type: 'success'
                });

            })
                .catch(function (error) {
                    console.log(error);
                });
        },
        //新增班级
        add() {

            var that = this
            that.$axios.post('/class/addclass', that.newclass).then(function (response) {
                console.log(response);
                that.dialogFormVisible = false
                that.pushdata()
                that.open1();
            })
                .catch(function (error) {
                    console.log(error);
                    that.open2();
                });


        }
    },
}
</script>

<style>
.select {

    display: flex;
    height: 100px;
    position: relative;
    top: 30px;
    left: 30px;
}

.block {
    position: absolute;
    bottom: 20px;
}

.context {
    position: relative;
    width: 1550px;
    height: 660px;
    margin: 20px auto;
    background-color: rgb(255 255 255);
    -webkit-box-shadow: 3px 3px 6px rgb(196, 189, 189);
    -moz-box-shadow: 3px 3px 6px rgb(196, 189, 189);
    box-shadow: 3px 3px 6px rgb(196, 189, 189);
}
</style>